package com.chen;

import cn.hutool.core.util.ObjectUtil;
import com.chen.mapper.UserDao;
import com.chen.pojo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class RunnerCommonLine implements CommandLineRunner {
    private static final Logger log = LoggerFactory.getLogger(RunnerCommonLine.class);
    @Resource
    private UserDao userDao;

    @Override
    public void run(String... args) throws Exception {
        // 初始化admin用户
        User admin = userDao.selectUserByUserName("admin");
        if (ObjectUtil.isEmpty(admin)) {
            // 初始化用户
            admin = new User();
            admin.setUsername("admin");
            admin.setPassword("123456");
            int i = userDao.insert(admin);
            if (i > 0) {
                log.info("[main] init user = {} success.", admin.getUsername());
            } else {
                log.info("[main] init user = {} fail.", admin.getUsername());
            }
        } else {
            log.info("[main] admin user is exits.");
        }

    }
}
